<!DOCTYPE html>

<html class="no-js">

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title></title>
  <meta name="description" content="" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />

  <script src="https://cdn.staticfile.org/axios/0.19.0-beta.1/axios.js"></script>

  <script src="../../cdn/vue/vue.global.min.js"></script>

  <link rel="stylesheet" href="../../cdn/element-plus/index.css" />
  <script src="../../cdn/element-plus/index.full.min.js"></script>
  <link rel="stylesheet" href="../../../lib/index.css" />
  <script src="../../../lib/avue.js"></script>
</head>

<body>
  <div id="app">
    <avue-tree @node-drag-start="handleDragStart" @node-drag-enter="handleDragEnter" @node-drag-leave="handleDragLeave"
      @node-drag-over="handleDragOver" @node-drag-end="handleDragEnd" @node-drop="handleDrop" :before-open="beforeOpen"
      :before-close="beforeClose" ref="tree" :filter-node-method="filterNodeMethod" :permission="getPermission"
      :option="option" :data="data" @node-click="nodeClick" @update="update" @save="save" @del="del" v-model="form">
      <template #="{ node, data }">
        <span class="el-tree-node__label">
          <el-icon>
            <user />
          </el-icon>
          {{ node.label }}
        </span>
      </template>
      <template #menu="scope">
        <div class="avue-tree__item" @click.native="tip(scope.node,scope.data)">自定义按钮</di>
      </template>
    </avue-tree>

  </div>
</body>
<script>
  var app = Vue.createApp({
    data() {
      return {
        form: {},
        data: [
          {
            value: 0,
            label: '一级部门',
            children: [
              {
                value: 1,
                label: '一级部门1',
              }, {
                value: 2,
                label: '一级部门2',
              }
            ]
          }, {
            value: 3,
            label: '二级部门',
            children: [
              {
                value: 4,
                label: '二级部门1',
              }, {
                value: 5,
                label: '二级部门2',
              }
            ]
          }
        ],
        option: {
          // virtualize: true,
          defaultExpandAll: true,
          dialogWidth: '50%',
          title: "新增部门",
          draggable: true,
          formOption: {
            labelWidth: 100,
            column: [{
              label: '自定义项',
              prop: 'test'
            }],
          },
          props: {
            labelText: '标题',
            label: 'label',
            value: 'value',
            children: 'children'
          }
        }
      }
    },
    methods: {
      handleDragStart(node, ev) {
        console.log('drag start', node);
      },
      handleDragEnter(draggingNode, dropNode, ev) {
        console.log('tree drag enter: ', dropNode.label);
      },
      handleDragLeave(draggingNode, dropNode, ev) {
        console.log('tree drag leave: ', dropNode.label);
      },
      handleDragOver(draggingNode, dropNode, ev) {
        console.log('tree drag over: ', dropNode.label);
      },
      handleDragEnd(draggingNode, dropNode, dropType, ev) {
        console.log('tree drag end: ', dropNode && dropNode.label, dropType);
      },
      handleDrop(draggingNode, dropNode, dropType, ev) {
        console.log('tree drop: ', dropNode.label, dropType);
      },
      beforeOpen(done, type) {
        alert(type);
        done()
      },
      beforeClose(done, type) {
        alert(type);
        done()
      },
      getPermission(key, data) {
        // if (key == "addBtn") { return false; }
        return true;
      },
      tip(node, data) {
        this.$message.success(JSON.stringify(data))
      },
      del(data, done) {
        this.$message.success('删除回调')
        done();
      },
      update(parent, data, done, loading) {
        console.log(parent);
        this.$message.success('更新回调')
        done();
      },
      save(parent, data, done, loading) {
        console.log(parent);
        this.$message.success('新增回调')
        this.form.value = new Date().getTime();
        done();
      },
      filterNodeMethod(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      nodeClick(data) {
        this.$message.success(JSON.stringify(data))
      }
    }
  })
  app.use(ElementPlus)
  app.use(AVUE)
  app.mount('#app')
</script>

</html>